package com.xbongbong.sys.help;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.pojo.AppPermissionPojo;
import com.xbongbong.paas.toolbox.pojo.ModulePermissionPojo;
import com.xbongbong.paas.toolbox.pojo.PermissionTermsPojo;
import com.xbongbong.paas.toolbox.util.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @date 2021/10/20 15:55
 */
public class SysProPermissionHelp {

    /**
     * paas权限取并集，取 permissions1 和 permissions2 的并集
     * 结构为数据库存储结构
     * @param permissions1 paas权限1
     * @param permissions2 paas权限2
     * @return permissions1 和 permissions2 的paas权限并集，转为字符串
     */
    public static String paasPermissionUnion(String permissions1, String permissions2) {
        if (StringUtil.isEmpty(permissions1)) {
            return permissions2;
        } else if (StringUtil.isEmpty(permissions2)) {
            return permissions1;
        }

        // 扫描以多项为循环体，少项变成Map，减少内存消耗
        List<AppPermissionPojo> appPermsMore;
        List<AppPermissionPojo> appPermsLess;
        if (permissions1.length() > permissions2.length()) {
            appPermsMore = JSON.parseArray(permissions1, AppPermissionPojo.class);
            appPermsLess = JSON.parseArray(permissions2, AppPermissionPojo.class);

            // 空权限处理
            if (appPermsLess == null || appPermsLess.isEmpty()) {
                return permissions1;
            }
        } else {
            appPermsMore = JSON.parseArray(permissions2, AppPermissionPojo.class);
            appPermsLess = JSON.parseArray(permissions1, AppPermissionPojo.class);

            // 空权限处理
            if (appPermsLess == null || appPermsLess.isEmpty()) {
                return permissions2;
            }
        }

        // appPermsLess 转为 Map<Long, List<ModulePermissionPojo>> 结构
        Map<Long, List<ModulePermissionPojo>> appPermsLessMap = new HashMap<>(appPermsLess.size());
        for (AppPermissionPojo appPermissionPojo : appPermsLess) {
            appPermsLessMap.put(appPermissionPojo.getAppId(), appPermissionPojo.getModules());
        }

        for (AppPermissionPojo appPerm : appPermsMore) {
            if (!appPermsLessMap.containsKey(appPerm.getAppId())) {
                continue;
            }
            List<ModulePermissionPojo> appPermsLessModules = appPermsLessMap.get(appPerm.getAppId());
            List<ModulePermissionPojo> appPermsMoreModules = appPerm.getModules();
            if (appPermsLessModules != null && appPermsLessModules.size() > 0) {
                // 取应用内表单权限并集
                modulePermsUnion(appPermsMoreModules, appPermsLessModules);
            }
            // 移除后保证剩下的部分是appPermsMore没有的权限
            appPermsLessMap.remove(appPerm.getAppId());
        }

        // 有剩余则增加到列表中
        if (appPermsLessMap.size() > 0) {
            for (Map.Entry<Long, List<ModulePermissionPojo>> entry: appPermsLessMap.entrySet()) {
                AppPermissionPojo addAppPerm = new AppPermissionPojo();
                addAppPerm.setAppId(entry.getKey());
                addAppPerm.setModules(entry.getValue());
                appPermsMore.add(addAppPerm);
            }
        }

        return JSON.toJSONString(appPermsMore);

    }

    /**
     * paas权限取并集，应用内取 appPermsMoreModules 和 appPermsLessModules 的并集，并集为appPermsMoreModules
     * @param appPermsMoreModules 并集操作因子1 并作为并集返回
     * @param appPermsLessModules 并集操作因子2
     */
    private static void modulePermsUnion(List<ModulePermissionPojo> appPermsMoreModules, List<ModulePermissionPojo> appPermsLessModules) {
        if (appPermsLessModules == null || appPermsLessModules.size() <= 0) {
            return;
        }

        for (ModulePermissionPojo modulePermLess : appPermsLessModules) {
            boolean findIt = false;
            for (ModulePermissionPojo modulePermMore : appPermsMoreModules) {
                if (modulePermLess.getId().equals(modulePermMore.getId())) {
                    PermissionTermsPojo maxPermission = modulePermMore.getPermission().maxPermission(modulePermLess.getPermission());
                    modulePermMore.setPermission(maxPermission);
                    findIt = true;
                    break;
                }
            }
            // 未找到说明是需要增加模块权限
            if (!findIt) {
                appPermsMoreModules.add(modulePermLess);
            }
        }
    }
}
